<script setup lang="ts">
import {Message} from "@arco-design/web-vue";
import {request} from "@/utils/request.js";
import tool from '@/utils/tool'
import AddModal from "@cps/actions/addModal.vue";
import ExeclTemplate from "@/views/template/excelExport/execlTemplate.vue";
import ExeclTemplateList from "@/views/template/excelExport/index.vue";
import {inject, reactive, ref} from "vue";

const crudRef = inject('crudRef');
const execlTemplate = ref();
const execlTemplateList = ref(false);
const props = defineProps({
  url: {type: String, required: true},
  type: {type: String, required: true},
  index: {type: String, default:'id'},
  params: {
    type: Object,
    default: () => {
      return {}
    }
  },
  ids: {
    type: String,
    default: 'ids'
  },
  auth:{
    type: Array,
    default: () => {
      return []
    }
  },
})
const addModalRef = ref();

const exportAction = (form) => {
  let params = {
    template_id: form.template_id
  }
  if(form.data_type == 1){
    let searchData = crudRef.value.requestParams
    // console.log(searchData)
    for(let i in searchData){
      if(searchData[i] && !['page','pageSize','scence','auditView'].includes(i)){
        params[i] = searchData[i]
      }
    }
  }
  if(form.data_type == 2){
    if(crudRef.value.selecteds.length == 0){
      Message.error('请选择要导出的数据')
      return
    }
    params[props.ids] = crudRef.value.selecteds
  }
  params['index'] = props.index
  Message.info('请求服务器下载文件中...')
  const data = Object.assign({}, props.params,params)
  const download = (url) => request({url, data, method: 'post', timeout: 60 * 1000, responseType: 'blob'})
  
  download(props.url).then(res => {
    tool.download(res)
    Message.success('请求成功，文件开始下载')
  })
}

const open = () => {
  addModalRef.value.open(reactive([
    {
      title: "选择数据",
      dataIndex: "data_type",
      formType: "radio",
      type: 'button',
      dict: {
        data: [
          {label: '搜索数据', value: 1},
          {label: '选择数据', value: 2}
        ]
      },
      addDefaultValue: 1,
      commonRules: {
        required: true,
        message: "请选择数据类型"
      },
    },
    {
      title: "模板名称",
      dataIndex: "template_id",
      formType: "select",
      dict: {
        url: 'template/excelExport/list',
        params: {object_type: props.type},
        props: {label: 'name', value: 'id'},
        afterLoadData:(dict,maFormObject)=>{
          if(dict[0]?.value){
            maFormObject.formModel.template_id = dict[0].value
          }
        }
      },
      addApi:()=>{
          execlTemplate.value.open()
      },
      listApi:()=>{
        execlTemplateList.value = true
      },
      addApiAuth:'template:excelExport:save',
      commonRules: {
        required: true,
        message: "请选择模板"
      },
    },
  ]), {
    viewType: 'modal',
    okText:'导出'
  }, exportAction,'导出')
}
</script>

<template>
  <a-button @click="open" v-auth="props.auth">
    <template #icon>
      <icon-download/>
    </template>
    导出
  </a-button>
  <add-modal ref="addModalRef" title="导出">
  </add-modal>
  <execl-template :config="props" ref="execlTemplate" />

  <a-modal :visible="execlTemplateList" width="80%" :footer="false" title="导出模板管理" @cancel="execlTemplateList = false">
    <ExeclTemplateList />
  </a-modal>
  
</template>

<style scoped>

</style>